package com.zdgcc.site.db;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import javax.servlet.http.HttpServletRequest;

public class ValueOfRequest {

	/**
	 * 从请求里得到表单值生成BIN对象
	 * 
	 * @param <T>
	 * @param request
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws UnsupportedEncodingException
	 */
	public static <T> Object createBinOfRequest(HttpServletRequest request,
			Class<T> clazz) {
		Object obj = null;
		Field[] fieldArray = clazz.getDeclaredFields();
		Field f = null;
		String value = null;
		try {
			obj = clazz.newInstance();
			for (int i = 0; i < fieldArray.length; i++) {
				f = fieldArray[i];
				Class<?> cls = f.getType();
				value = request.getParameter(f.getName());
				if (value != null) {
					boolean flag = f.isAccessible();
					f.setAccessible(true);
					f.set(obj, valueOf(cls, value));
					f.setAccessible(flag);
				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return obj;
	}

	/**
	 * 把字符串赋值给执行类
	 * 
	 * @param cls
	 * @param value
	 * @return object
	 */
	public static Object valueOf(Class<?> cls, String value) {
		Object obj = Types.OTHER;
		try {
			// 如遇基本数据类型, 会抛异常, 比如Integer可以实例化, 而int和long都不能
			obj = cls.newInstance();
			if (obj instanceof Boolean) {
				obj = Boolean.valueOf(value);
			} else if (obj instanceof Byte) {
				obj = Byte.valueOf(value);
			} else if (obj instanceof Date) {
				obj = Date.valueOf(value);
			} else if (obj instanceof Double) {
				obj = Double.valueOf(value);
			} else if (obj instanceof Float) {
				obj = Float.valueOf(value);
			} else if (obj instanceof Integer) {
				obj = Integer.valueOf(value);
			} else if (obj instanceof Long) {
				obj = Long.valueOf(value);
			} else if (obj instanceof Short) {
				obj = Short.valueOf(value);
			} else if (obj instanceof String) {
				obj = String.valueOf(value);
			} else if (obj instanceof Time) {
				obj = Time.valueOf(value);
			} else if (obj instanceof Timestamp) {
				obj = Timestamp.valueOf(value);
			}
		} catch (InstantiationException e) {
			// 基本数据类型的赋值
			String sType = cls.getName();
			try {
				if (sType == "byte") {
					obj = Byte.parseByte(value);
				} else if (sType == "int" || sType == "java.lang.Integer") {
					obj = Integer.parseInt(value);
				} else if (sType == "long" || sType == "java.lang.Long") {
					obj = Long.parseLong(value);
				} else if (sType == "float") {
					obj = Float.parseFloat(value);
				} else if (sType == "double") {
					obj = Double.parseDouble(value);
				} else if (sType == "java.math.BigDecimal") {
					if (value.indexOf('.') < 0) {
						obj = Long.parseLong(value);
					} else {
						obj = Double.parseDouble(value);
					}
				}
			} catch (NumberFormatException ne) {
				ne.printStackTrace();
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return obj;
	}

}
